from flask import Flask, render_template
import util
from flask import jsonify
import re


app = Flask(__name__)


@app.route('/')
def hello_world():
    return render_template("main.html")


@app.route("/time")
def get_time():
    print(util.get_time())
    return util.get_time()


@app.route('/c2')
def get_c2_data():
    res1 = []
    for tup in util.get_c2_data():
        res1.append({"name":tup[0],"value":int(tup[1])})
    return jsonify({"data":res1})


@app.route('/l1')
def get_l1_data():
    data = util.get_l1_data()
    b3 = u'万/年'
    b4 = u'千/月'
    required,salary = [], []
    for i in range(0, len(data)):
        a = data[i][0]
        b = data[i][1]
        try:
            if b3 in b:
                x = re.findall(r'\d*\.?\d+', b)
                min_ = format(float(x[0]) / 12, '.2f')  # 转换成浮点型并保留两位小数
                max_ = format(float(x[1]) / 12, '.2f')
                y = min_ + '-' + max_ + u'万/月'
            if b4 in b:
                x = re.findall(r'\d*\.?\d+', b)
                min_ = format(float(x[0]) / 10, '.2f')
                max_ = format(float(x[1]) / 10, '.2f')
                y = str(min_ + '-' + max_ + '万/月')
            else:
                y = b
            salary1 = re.findall(r'\d*\.?\d+', y)
            salary2 = format((float(salary1[0]) + float(salary1[1])) / 2 * 10000, '.2f')
            salary.append(salary2)
        except:
            pass
        required.append(a)

    salarys = []
    b = []
    for i in required:
        if i not in b:
            b.append(i)
    for i in range(0, len(b)):
        salary3 = 0
        for y in range(0, len(required)):
            if b[i] == required[y]:
                salary3 = format((float(salary3) + float(salary[y]))/2,'.2f')
        salarys.append(salary3)
    b2 = u'人'
    data_required,data_salary = [], []
    for i in range(0, len(b)):
        try:
            if b2 not in b[i]:
                data_required.append(b[i])
                data_salary.append(salarys[i])
        except:
            pass
    return jsonify({"required": data_required, "salary": data_salary})


@app.route('/l2')
def get_l2_data():
    b2 = u'经验'
    li2 = util.get_l2_data()
    data_l2 = []
    for i in range(0, len(li2)):
        try:
            if b2 in li2[i][0]:
                data_l2.append(li2[i])
        except:
            pass
    res2 = []
    for tup in data_l2:
        res2.append({"value":int(tup[1]),"name":tup[0]})
    return jsonify({"data":res2})


@app.route('/r1')
def get_r1_data():
    data = util.get_r1_data()
    b3 = u'万/年'
    b4 = u'千/月'
    recruiters,salary = [], []
    for i in range(0, len(data)):
        a = data[i][0]
        b = data[i][1]
        try:
            if b3 in b:
                x = re.findall(r'\d*\.?\d+', b)
                min_ = format(float(x[0]) / 12, '.2f')  # 转换成浮点型并保留两位小数
                max_ = format(float(x[1]) / 12, '.2f')
                y = min_ + '-' + max_ + u'万/月'
            if b4 in b:
                x = re.findall(r'\d*\.?\d+', b)
                min_ = format(float(x[0]) / 10, '.2f')
                max_ = format(float(x[1]) / 10, '.2f')
                y = str(min_ + '-' + max_ + '万/月')
            else:
                y = b
            salary1 = re.findall(r'\d*\.?\d+', y)
            salary2 = format((float(salary1[0]) + float(salary1[1])) / 2 * 10000, '.2f')
            salary.append(salary2)
        except:
            pass
        recruiters.append(a)

    salarys = []
    b = []
    for i in recruiters:
        if i not in b:
            b.append(i)
    for i in range(0, len(b)):
        salary3 = 0
        for y in range(0, len(recruiters)):
            if b[i] == recruiters[y]:
                salary3 = format((float(salary3) + float(salary[y]))/2,'.2f')
        salarys.append(salary3)
    b2 = u'经验'
    data_recruiters,data_salary = [], []
    for i in range(0, len(b)):
        try:
            if b2 in b[i]:
                data_recruiters.append(b[i])
                data_salary.append(salarys[i])
        except:
            pass
    return jsonify({"recruiters": data_recruiters, "salary": data_salary})


@app.route('/r2')
def get_r2_data():
    b2 = u'人'
    li2 = util.get_r2_data()
    data_r1 = []
    for i in range(0, len(li2)):
        try:
            if b2 not in li2[i][0]:
                data_r1.append(li2[i])
        except:
            pass
    res3 = []
    for tup in data_r1:
        res3.append({"value":int(tup[1]),"name":tup[0]})
    return jsonify({"data":res3})


if __name__ == '__main__':
    app.run()

